package com.zzjc.web.util;


import java.math.BigDecimal;
import java.security.MessageDigest;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class StringKit {
    static SimpleDateFormat df_sdf1 = new SimpleDateFormat("yyyy-MM-dd");
    static SimpleDateFormat df_sdf2 = new SimpleDateFormat("yyyy-MM");
    static DecimalFormat nb_ft = new DecimalFormat("0.00");

    public static String firstCharToLowerCase(String str)
    {
        Character firstChar = Character.valueOf(str.charAt(0));
        String tail = str.substring(1);
        str = Character.toLowerCase(firstChar.charValue()) + tail;
        return str;
    }
    public static String firstCharToUpperCase(String str)
    {
        Character firstChar = Character.valueOf(str.charAt(0));
        String tail = str.substring(1);
        str = Character.toUpperCase(firstChar.charValue()) + tail;
        return str;
    }

    public static boolean isInt(String str)
    {
        boolean isNumber = false;
        if (notBlank(str))
        {
            str.matches("^([1-9]\\d*)|(0)$");
            isNumber = true;
        }
        return isNumber;
    }

    public static boolean isDouble(String str)
    {
        boolean isNumber = false;
        if ((notBlank(str)) && (str.matches("^-?\\d+(\\.\\d+)?$"))) {
            isNumber = true;
        }
        return isNumber;
    }

    public static boolean isBlank(Object str)
    {
        return (str == null) || ("".equals(str.toString().trim())) || ("null".equalsIgnoreCase(str.toString().trim()));
    }

    public static boolean notBlank(Object str)
    {
        return !isBlank(str);
    }

    public static boolean notBlank(String... strings)
    {
        if (strings == null) {
            return false;
        }
        String[] arrayOfString = strings;int j = strings.length;
        for (int i = 0; i < j; i++)
        {
            String str = arrayOfString[i];
            if (isBlank(str)) {
                return false;
            }
        }
        return true;
    }

    public static boolean notNull(Object... paras)
    {
        if (paras == null) {
            return false;
        }
        Object[] arrayOfObject = paras;int j = paras.length;
        for (int i = 0; i < j; i++)
        {
            Object obj = arrayOfObject[i];
            if (obj == null) {
                return false;
            }
        }
        return true;
    }

    public static String arrayToString(Object[] strs, String decollator)
    {
        StringBuffer str = new StringBuffer();
        for (int i = 0; i < strs.length; i++) {
            str.append(strs[i] + decollator);
        }
        if (str.length() > 0) {
            str.deleteCharAt(str.length() - decollator.length());
        }
        return str.toString();
    }

    public static String arrayIDToString(Object[] objs)
    {
        int num = 0;
        StringBuffer string = new StringBuffer();
        Object[] arrayOfObject = objs;int j = objs.length;
        for (int i = 0; i < j; i++)
        {
            Object obj = arrayOfObject[i];
            if (!isBlank(obj))
            {
                String temp = obj.toString();
                temp = temp.trim().replace("'", "").replace("\"", "");
                if (num == 0)
                {
                    string.append("'" + temp + "'");
                    num++;
                }
                else
                {
                    string.append(",'" + temp + "'");
                }
            }
        }
        return string.toString();
    }

    public static String arrayIDToString(Collection collection)
    {
        if ((collection == null) || (collection.size() == 0)) {
            return "";
        }
        return arrayIDToString(collection.toArray());
    }

    public static String change_in(String stringByComma)
    {
        return change_in(stringByComma, ",");
    }

    public static String change_in(String stringByComma, String decollator)
    {
        if ((stringByComma == null) || (stringByComma.equals(""))) {
            return "''";
        }
        String[] strings = stringByComma.trim().split(decollator);
        return arrayIDToString(strings);
    }

    public static String transitionNum(double num)
    {
        DecimalFormat dFormat = new DecimalFormat("0.0");
        if ((num < 10000.0D) && (num > -10000.0D)) {
            return dFormat.format(num);
        }
        num /= 10000.0D;
        if ((num / 10000.0D > 1.0D) || (num / 10000.0D < -1.0D)) {
            return dFormat.format(num / 10000.0D) + "亿";
        }
        return dFormat.format(num) + "万";
    }

    public static String replaceBlank(String str)
    {
        String dest = "";
        if (str != null)
        {
            Pattern p = Pattern.compile("\\s*|\t|\r|\n");
            Matcher m = p.matcher(str);
            dest = m.replaceAll("");
        }
        return dest;
    }

    public static boolean changeToBoolean(String boolean_str)
    {
        if (notBlank(boolean_str))
        {
            if (boolean_str.trim().equals("true")) {
                return true;
            }
            return false;
        }
        return false;
    }

    public static String toUpperStr(String str)
    {
        if (isBlank(str)) {
            return "";
        }
        return str.toUpperCase();
    }

    public static String toLowerStr(String str)
    {
        if (isBlank(str)) {
            return "";
        }
        return str.toLowerCase();
    }

    public static int parseToInt(String str)
            throws NumberFormatException
    {
        if (isBlank(str)) {
            return 0;
        }
        return Integer.parseInt(str);
    }

    public static double parseToDouble(String str)
            throws NumberFormatException
    {
        if (isBlank(str)) {
            return 0.0D;
        }
        return Double.parseDouble(str);
    }
    public static Double parseToDouble(Object num)
    {
        if (isBlank(num)) {
            return Double.valueOf(0.0D);
        }
        if ((num instanceof Double)) {
            return (Double)num;
        }
        if ((num instanceof BigDecimal)) {
            return Double.valueOf(((BigDecimal)num).doubleValue());
        }
        if ((num instanceof Number)) {
            return Double.valueOf(((Number)num).doubleValue());
        }
        return new Double(parseToDouble(num.toString()));
    }

    public static String ValueOf(Object obj)
    {
        if (obj == null) {
            return "";
        }
        return obj.toString();
    }

    public static String formatDouble_str(double value)
    {
        return nb_ft.format(value);
    }

    public static double formatDouble_num(double value)
    {
        return Double.parseDouble(nb_ft.format(value));
    }



    public static String delRepeatByStrNodyh(String str)
    {
        String result = "";
        if (notBlank(str))
        {
            String[] list = str.split(",");
            for (int i = 0; i < list.length; i++) {
                if (result.indexOf(list[i]) == -1) {
                    result = result + list[i] + ",";
                }
            }
        }
        if (notBlank(str)) {
            result = result.substring(0, result.length() - 1);
        }
        return result;
    }

    public static String searchKey(String s)
    {
        if ("eq".equals(s)) {
            return "=";
        }
        if ("ne".equals(s)) {
            return "!=";
        }
        if ("gt".equals(s)) {
            return ">";
        }
        if ("ge".equals(s)) {
            return ">=";
        }
        if ("lt".equals(s)) {
            return "<";
        }
        if ("le".equals(s)) {
            return "<=";
        }
        if ("cn".equals(s)) {
            return "like";
        }
        if ("bw".equals(s)) {
            return "begin like";
        }
        if ("ew".equals(s)) {
            return "end like";
        }
        if ("in".equals(s)) {
            return "in";
        }
        if ("ni".equals(s)) {
            return "not in";
        }
        return "=";
    }

    public static String MD5(String s)
    {
        char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'a', 'b', 'c', 'd', 'e', 'f' };
        try
        {
            byte[] strTemp = s.getBytes();
            MessageDigest mdTemp = MessageDigest.getInstance("MD5");
            mdTemp.update(strTemp);
            byte[] md = mdTemp.digest();
            int j = md.length;
            char[] str = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++)
            {
                byte byte0 = md[i];
                str[(k++)] = hexDigits[(byte0 >>> 4 & 0xF)];
                str[(k++)] = hexDigits[(byte0 & 0xF)];
            }
            return new String(str);
        }
        catch (Exception e) {}
        return null;
    }

    public static List<Object> getTheSameSection(List<Object> list1, List<Object> list2)
    {
        List<Object> list = new ArrayList();
        if ((list1 != null) && (list1.size() > 0) && (list2 != null) && (list2.size() > 0)) {
            for (Object o : list1) {
                if (list2.contains(o)) {
                    list.add(o);
                }
            }
        }
        return list;
    }

    public static int indexOfArr(String[] arr, String value2)
    {
        if ((arr == null) || (arr.length == 0)) {
            return -1;
        }
        for (int i = 0; i < arr.length; i++) {
            if (arr[i].equals(value2)) {
                return i;
            }
        }
        return -1;
    }

    public static String nvl(Object object)
    {
        String retString = "";
        if (object != null) {
            retString = object.toString().trim();
        }
        return retString;
    }

    public static int iNull(Object obj)
    {
        if (obj == null) {
            return 0;
        }
        if ((obj instanceof Integer)) {
            return ((Integer)obj).intValue();
        }
        String str = obj.toString().trim();
        if (str.equals("")) {
            return 0;
        }
        return Integer.parseInt(str.split("\\.")[0]);
    }

    public static BigDecimal parseToBigDecimal(String str)
    {
        try
        {
            return new BigDecimal(str);
        }
        catch (Exception e) {}
        return null;
    }

    public static String htmlEncode(String str)
    {
        if (notBlank(str))
        {
            str = str.replaceAll("&", "&amp;");
            str = str.replaceAll("<", "&lt;");
            str = str.replaceAll(">", "&gt;");
            str = str.replaceAll("\"", "&quot;");
        }
        return str;
    }

    public static BigDecimal stringToBigDecimal(String data)
    {
        if (isBlank(data)) {
            data = "0";
        }
        BigDecimal bd = new BigDecimal(data);
        return bd;
    }

    public static String tointStr(Object object)
    {
        String retString = "0";
        if (object != null) {
            retString = object.toString().trim();
        }
        return retString;
    }

    public static double getObjToDouble(Object b)
    {
        try
        {
            return Double.parseDouble(String.valueOf(b));
        }
        catch (Exception e) {}
        return 0.0D;
    }



    public static int parseToInt2(String str)
    {
        if (isBlank(str)) {
            return 0;
        }
        try
        {
            return Integer.parseInt(str);
        }
        catch (NumberFormatException e) {}
        return 0;
    }

    public static String getNotNullString(Object obj)
    {
        if (obj == null) {
            return "";
        }
        return String.valueOf(obj).trim();
    }

    public static Object[] changeStrToLongBySplit(String str)
    {
        if (!notBlank(str)) {
            return new Object[0];
        }
        String[] ids = str.split(",");
        List<Long> list = new ArrayList();
        for (String id : ids) {
            list.add(Long.valueOf(Long.parseLong(id)));
        }
        return list.toArray();
    }
    public static String delZero(String src)
    {
        return
                BigDecimal.valueOf(Double.parseDouble(src)).stripTrailingZeros().toPlainString();
    }
    public static String getMapToString(Map<String,Object> map){
        Set<String> keySet = map.keySet();
        //将set集合转换为数组
        String[] keyArray = keySet.toArray(new String[keySet.size()]);
        //给数组排序(升序)
        Arrays.sort(keyArray);
        //因为String拼接效率会很低的，所以转用StringBuilder
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < keyArray.length; i++) {
            // 参数值为空，则不参与签名 这个方法trim()是去空格
            if ((String.valueOf(map.get(keyArray[i]))).trim().length() > 0) {
                sb.append(keyArray[i]).append(":").append(String.valueOf(map.get(keyArray[i])).trim());
            }
            if(i != keyArray.length-1){
                sb.append(",");
            }
        }
        return sb.toString();
    }
}

